package com.yuan.model_community.rest;


import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_community.domain.CommunityInfo;
import com.yuan.model_community.service.CommunityInfoService;
import com.yuan.model_community.service.dto.CommunityInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Api(tags = "物业便民: 小区信息管理")
@RestController
@RequestMapping("/api/communityInfo")
@RequiredArgsConstructor
public class CommunityInfoController {

    private final CommunityInfoService communityInfoService;

    @ApiOperation("导出/community/info")
    @GetMapping(value = "/download")
    public void exportCInfo(HttpServletResponse response, CommonQueryCriteria criteria) throws IOException {
        communityInfoService.download(communityInfoService.queryAll(criteria), response);
    }

    @GetMapping("/findList")
    @Log("查询/api/community/info")
    @ApiOperation("查询/api/community/info")
    public ResponseEntity<List<CommunityInfoDto>> queryCInfoList() {
        List<CommunityInfoDto> communityInfoDtos = communityInfoService.queryAll();
        return new ResponseEntity<>(communityInfoDtos, HttpStatus.OK);
    }


    @Log("新增/api/community/info")
    @ApiOperation("新增/api/community/info")
    @PostMapping("/create")
    public ResponseEntity<Object> createCInfo(@Validated @RequestBody CommunityInfo resources) {
        communityInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改/api/community/info")
    @ApiOperation("修改/api/community/info")
    @PostMapping("/update")
    public ResponseEntity<Object> updateCInfo(@RequestParam Integer id, @Validated(CommunityInfo.Update.class) @RequestBody CommunityInfo resources) throws Exception {
        communityInfoService.update(id, resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除/api/community/info")
    @ApiOperation("删除/api/community/info")
    @PostMapping("/delete")
    public ResponseEntity<Object> deleteCInfo(@RequestParam Integer ids) {
        communityInfoService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
